package utility.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

import sun.net.ftp.FtpClient;

public class Upload2Cach {

	private static final String SERVER = "localhost";
	private static final int PORT = 21;
	private static final String USER = "anonymous";
	private static final String PASSWORD = "";

	FtpClient ftper = null;

	public static void main(String[] args) {
		String server = "localhost";
		int port = 21;
		String user = "anonymous";
		String pass = "";

		FTPClient ftpClient = new FTPClient();
		try {

			ftpClient.connect(server, port);
			boolean b = ftpClient.login(user, pass);

			ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

			// APPROACH #1: uploads first file using an InputStream
			String fileStr = "/var/adpv6/listing/upload/2013-08-28/2/creativeHeld_2_2013-08-28_1377666438.txt";
			File firstLocalFile = new File(fileStr);
			int i = fileStr.lastIndexOf("/");
			String name = fileStr.substring(i + 1);

			String firstRemoteFile = name;
			InputStream inputStream = new FileInputStream(firstLocalFile);

			System.out.println("Start uploading first file");
			boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
			inputStream.close();
			if (done) {
				System.out.println("The first file is uploaded successfully.");
			}
			/*
			 * // APPROACH #2: uploads second file using an OutputStream File
			 * secondLocalFile = new File("E:/Test/Report.doc"); String
			 * secondRemoteFile = "test/Report.doc"; inputStream = new
			 * FileInputStream(secondLocalFile);
			 *
			 * System.out.println("Start uploading second file"); OutputStream
			 * outputStream = ftpClient.storeFileStream(secondRemoteFile);
			 * byte[] bytesIn = new byte[4096]; int read = 0;
			 *
			 * while ((read = inputStream.read(bytesIn)) != -1) {
			 * outputStream.write(bytesIn, 0, read); } inputStream.close();
			 * outputStream.close();
			 *
			 * boolean completed = ftpClient.completePendingCommand(); if
			 * (completed) {
			 * System.out.println("The second file is uploaded successfully.");
			 * }
			 */
		} catch (IOException ex) {
			System.out.println("Error: " + ex.getMessage());
			ex.printStackTrace();
		} finally {
			try {
				if (ftpClient.isConnected()) {
					ftpClient.logout();
					ftpClient.disconnect();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}

	private FTPClient ftpLogin() {
		try {
			FTPClient ftper = new FTPClient();
			ftper.connect(SERVER, PORT);
			boolean login = ftper.login(USER, PASSWORD);
			return login ? ftper : null;
		} catch (Exception e) {
			return null;
		} finally {
		}
	}

	private boolean ftpUpload(FTPClient ftper, String filePath) {
		int index = filePath.lastIndexOf("/");
		String fileName = filePath.substring(index + 1);
		try {
			File file = new File(filePath);
			InputStream inputStream = new FileInputStream(file);
			boolean result = ftper.storeFile(fileName, inputStream);
			inputStream.close();
			return result;
		} catch (Exception e) {
			return false;
		} finally {
		}
	}

	private void ftpLogout() {
		try {
			//if (ftper != null && ftper.isConnected()) {
			//	ftper.logout();
			//	ftper.disconnect();
			//}
		} catch (Exception e) {
		}
	}

}
